import { ExtendCalendarPropsType } from '../extend-calendar.props';
import { SetupContext,Ref } from 'vue';
import {UseDateMethods} from './types'

const isLeapYear = (year: number):number => {
    if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
        return 1;
    } else {
        return 0;
    }
}

const addZero = (n: number):number|string => {
    if (n < 10) {
        return '0' + n;
    } else {
        return n;
    }
}

const formatDate = (date: Date):string => {
    let date_ = new Date(date);
    let y = date_.getFullYear();
    let m = date_.getMonth() + 1;
    let d = date_.getDate();
    return `${addZero(y)}-${addZero(m)}-${addZero(d)}`
}

const isDate = (item: any, od: any):boolean => {
    let d = new Date(od);
    if (item.day === d.getDate() && item.month === d.getMonth() && item.year === d.getFullYear()) {
        return true;
    } else {
        return false;
    }
}

export default function (
    props: ExtendCalendarPropsType,
    context: SetupContext,
):UseDateMethods {
    const computedDate = (date: Date, list: any[], beginWeek: number,selected:Ref) => {
        let count = 7 * 6;
        let oDate = new Date(date);
        let year = oDate.getFullYear();
        let month = oDate.getMonth();
        let day = oDate.getDate();
        let days = ([31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]);
        let firstDayLeft = new Date(year, month, 1).getDay();     //当前时间的月份第一天周几
        let firstDay = (firstDayLeft == 0 ? 7 : firstDayLeft);
        let hovers = list;

        let beginDate = [];
        let start_y = year;
        let end_y = year;
        let start_m = month;
        let end_m = month;
        start_m -= 1;
        if (start_m < 0) {
            start_m = 11;
            start_y = start_y - 1;
        }
        let start_i_minus = 1;
        // 判断日历 周第一个是 周日 还是 周一
        if (beginWeek === 1) {
            start_i_minus = 1;
        }
        if (beginWeek === 7) {
            start_i_minus = 0;
        }
        if (beginWeek != 7 && beginWeek != 1) {
            throw new Error('beginWeek只能传入1和7');
        }
        for (let i = 1; i <= days[start_m]; i++) {
            if (i - start_i_minus > days[start_m] - firstDay) {
                beginDate.push({
                    status: -1,
                    day: i,
                    active: false,
                    year: start_y,
                    month: start_m,
                })
            }
        }
        if(beginDate.length === 7){
            beginDate.length = 0;
        }

        let mainDate = [];
        for (let i = 1; i <= days[month]; i++) {
            let itemDay = {
                status: 1,
                day: i,
                active: false,
                year: year,
                month: month,
            }
            if (day == i) {
                itemDay.active = true
                selected.value = Math.ceil((i + beginDate.length) / 7);
            } else {
                itemDay.active = false
            }
            mainDate.push(itemDay)
        }

        let endDate = [];
        end_m += 1;
        if (end_m > 11) {
            end_m = 0;
            end_y = end_y + 1;
        }
        let end_days = count - (beginDate.length + mainDate.length)
        for (let i = 0; i < end_days; i++) {
            endDate.push({
                status: 0,
                day: i + 1,
                active: false,
                year: end_y,
                month: end_m,
            })
        }
        if(endDate.length >= 7){
            endDate.length = endDate.length - 7;
        }
        
        let wholeDate = beginDate.concat(mainDate).concat(endDate);
        let wholeDateToWeeks:any[] = [];
        wholeDate.forEach((item: any, key) => {
            let dateStr = item.year + '-' + addZero(item.month + 1) + '-' + addZero(item.day);
            item['dateStr'] = dateStr;
            hovers.forEach((hover: any) => {
                if (isDate(item,hover.date)) {
                    item['task'] = hover;
                }
            })
            if (key % 7 == 0 && key >= 0) {
                wholeDateToWeeks.push([])
            }
            wholeDateToWeeks[wholeDateToWeeks.length - 1].push(item);
        })
        return wholeDateToWeeks;
    }
    return {
        computedDate
    }
}